package report.design.mainframe;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

public class JReportInternalFrame extends JInternalFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextArea ja;

	public JReportInternalFrame() {
		ja = new JTextArea();
		ja.setEditable(true);
		ja.setVisible(true);
		
		JScrollPane jsp = new JScrollPane(ja);
		jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//		JScrollBar bar = jsp.getVerticalScrollBar();
//		bar.setMinimum(bar.getMinimum());
		this.add(jsp);

		this.setResizable(true);
		this.setClosable(true);
		this.setMaximizable(true);
		this.setIconifiable(true);
		this.setResizable(true);

		this.setPreferredSize(new Dimension(1350, 638));
		this.setVisible(true);
	}

	public void save() {
		String a = ja.getText();
		try {
			JFileChooser file = new JFileChooser("C:\\Users\\neil\\Desktop");
			int result = file.showSaveDialog(null);

			if (result == JFileChooser.APPROVE_OPTION) {
				String path = file.getSelectedFile().getAbsolutePath();
			
				FileOutputStream fos = new FileOutputStream(path);
	
				OutputStreamWriter osw = new OutputStreamWriter(fos);
	
				BufferedWriter bw = new BufferedWriter(osw);
	
				bw.write(a);
	
				bw.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void open() {
		StringBuffer a = new StringBuffer();
		try {
			JFileChooser file = new JFileChooser("C:\\Users\\neil\\Desktop");
			int result = file.showOpenDialog(null);

			if (result == JFileChooser.APPROVE_OPTION) {
				String path = file.getSelectedFile().getAbsolutePath();

				FileInputStream fis = new FileInputStream(path);

				InputStreamReader isr = new InputStreamReader(fis);

				BufferedReader br = new BufferedReader(isr);

				String temp = br.readLine();

				while (temp != null) {
					a.append(temp).append("\n");
					temp = br.readLine();
				}
				ja.setText(a.toString());
				ja.setCaretPosition(0); 
				br.close();
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	//
	// public static void main(String[] args) {
	// JFrame jf = new JFrame();
	// jf.add(new JReportInternalFrame());
	// jf.setBounds(0, 0, 1366, 768);
	// jf.setVisible(true);
	// }
}